var showQuestionEditMap = {}; // 问题正在编辑中
var selectQuestionId = 0;

// 对问题进行编辑
function showLeftQuestion(e) {
    if (!checkWordUrl()) {
        return
    }
    if (showQuestionEditMap[e] != undefined && showQuestionEditMap[e] == 1) {
        return
    } else {
        showQuestionEditMap[e] = 1; // 正在修改标志位
    }
    selectQuestionId = e; // 记录被选中的问题ID
    $("#q_copyquestion_" + e).unbind("click").bind("click",  // 复制绑定事件
    function(f) {
        copyQuestion(e); // 
        f.stopPropagation(); //防止冒泡
    });
    $("#q_deletequestion_" + e).unbind("click").bind("click",  // 删除绑定事件
    function(f) {
        deleteQuestion(e);
        f.stopPropagation(); //防止冒泡
    });
    targetid = e; // 目标问题ID
    var d = $("#question_" + e).offset().top;
    selectFieldCss(); // 样式变动
    $("[id^=question_]").removeClass("editing").removeClass("focused");
    $("[id^=question_] > .fieldActions").css("visibility", "hidden");
    $("#question_" + e).addClass("editing").addClass("focused");
    $("#question_" + e + " > .fieldActions").css("visibility", "visible");
    var c = questionMap[e]; // 选出问题
    var b = getLeftShowHtml(c);
    $("#qeditshownone").hide();
    $("#qeditshow").show();
    $("#listButtons").show();
    $("#qeditshow").html(b);
    $("[id^=question_]").each(function(f) {
        var g = $(this).attr("id").split("_")[1];
        var h = 1;
        if (g == c.id) {
            h = 2
        }
        $(this).css("z-index", h)
    });
    bindQuestionUploadPic(c); // 绑定上传事件
    var a = (d < fieldtop) ? 0: (d - 140 < fieldtop ? 0: d - 140 - fieldtop);
    if (c.showtype == "page") {
        a = (d < fieldtop) ? 0: d - fieldtop
    }
    $("#qeditshow").css("margin-top", a);
    if (c.type == 6 && c.typeid == 9) {
        showDate(e)
    }
    if (c.type == 6 && c.typeid == 10) {
        showTime(e)
    }
    showFieldsPosition();
    showEditor(c);
    showSort(c);
    setTimeout(function() {
        showQuestionEditMap[e] = 0
    },
    500)
}
function showLeftQuestionMouseOver(a) {
    if (a == selectQuestionId && $("#question_" + a).hasClass("editing")) {
        return
    }
    $("#q_copyquestion_" + a).unbind("click").bind("click", 
    function(b) {
        copyQuestion(a);
        b.stopPropagation()
    });
    $("#q_deletequestion_" + a).unbind("click").bind("click", 
    function(b) {
        deleteQuestion(a);
        b.stopPropagation()
    });
    $("#question_" + a + " > .fieldActions").css("visibility", "visible")
}
function showLeftQuestionMouseOut(a) {
    if (a == selectQuestionId && $("#question_" + a).hasClass("editing")) {
        return
    }
    $("#q_copyquestion_" + a).unbind("click").bind("click", 
    function(b) {
        copyQuestion(a);
        b.stopPropagation()
    });
    $("#q_deletequestion_" + a).unbind("click").bind("click", 
    function(b) {
        deleteQuestion(a);
        b.stopPropagation()
    });
    $("#question_" + a + " > .fieldActions").css("visibility", "hidden")
}
function showEditor(b) {
    if (b.showtype != "page") {
        if (UEditor != null) {
            UEditor.destroy()
        }
        var c = null;
        if (b.type == 0) {
            c = ["bold", "forecolor", "justifyleft", "justifycenter", "justifyjustify", "fontsize", "fontfamily", "uploadimg", "removeformat"]
        } else {
            c = ["bold", "forecolor", "fontsize", "fontfamily", "uploadimg", "removeformat"]
        }
        var a = {
            initialContent: "",
            UEDITOR_HOME_URL: ctx + "/js/ueditor/",
            minFrameHeight: 60,
            zIndex: 1,
            toolbars: [c],
            contextMenu: [],
            wordCount: false,
            elementPathEnabled: false,
            labelMap: {
                uploadimg: "图片"
            }
        };
        UEditor = new UE.ui.Editor(a);
        UEditor.render("qleftdescriptionedit");
        UEditor.commands.uploadimg = {
            execCommand: function() {
                showLeftImg(b.id)
            }
        };
        UEditor.ready(function() {
            UEditor.addListener("selectionChange", 
            function() {
                saveQDescription(b.id, false)
            });
            UEditor.addListener("contentChange", 
            function() {
                saveQDescription(b.id, false)
            });
            UEditor.addListener("focus", 
            function() {
                checkQDescription(b.id)
            });
            UEditor.addListener("keyup", 
            function(e, d) {
                var f = d.keyCode || d.which;
                if (f == 88) {
                    saveQDescription(b.id, false)
                }
            })
        });
        if (b.type == 0) {
            if (intro_UEditor != null) {
                intro_UEditor.destroy()
            }
            var c = ["bold", "forecolor", "justifyleft", "justifycenter", "fontsize", "fontfamily", "removeformat"];
            var a = {
                initialContent: "",
                UEDITOR_HOME_URL: ctx + "/js/ueditor/",
                minFrameHeight: 60,
                zIndex: 1,
                toolbars: [c],
                contextMenu: [],
                wordCount: false,
                elementPathEnabled: false
            };
            intro_UEditor = new UE.ui.Editor(a);
            intro_UEditor.render("qlefteditinstruction");
            intro_UEditor.ready(function() {
                intro_UEditor.addListener("selectionChange", 
                function() {
                    saveQInstruction(b.id)
                });
                intro_UEditor.addListener("contentChange", 
                function() {
                    saveQInstruction(b.id)
                });
                intro_UEditor.addListener("keyup", 
                function(e, d) {
                    var f = d.keyCode || d.which;
                    if (f == 88) {
                        saveQInstruction(b.id)
                    }
                });
                if (b.instruction == null || b.instruction == "") {
                    setTimeout(function() {
                        intro_UEditor.setDisabled("fullscreen")
                    },
                    100)
                }
            })
        }
    }
}
function checkQDescription(c) {
    var b = questionMap[c];
    var a = getDefaultQuestion(b);
    if (a != null && a.id != null && b.description == $("#q_name_" + c).html() && a.description == b.description && b.isedit != 1) {
        setTimeout(function() {
            UEditor.execCommand("selectall")
        },
        100)
    }
}
function showSort(a) {
    if (a.type == 41 || a.type == 42) {
        showAnswerSort(a);
        showQuestionSort(a)
    }
    if (a.typeid == 1 && (a.type == 1 || a.type == 2)) {
        showAnswerSort(a)
    }
    if (a.typeid == 5 && a.type == 6) {
        showAnswerSort(a)
    }
}
function showAnswerSort(a) {
    $("#qleftanswers").sortable({
        cancel: ".dropReq,.nodrag,:input",
        handle: ".faDel",
        revert: true,
        update: function(d, f) {
            var c = $("[id^=qleftanswer_" + a.id + "_]").length;
            var g = $("#qleftanswers").find(".dropReq").length;
            if (g > 0) {
                if ($("[id^=qleftanswer_" + a.id + "_]").eq(c - 1).attr("id") != $("#qleftanswers").find(".dropReq").attr("id")) {
                    $(this).sortable("cancel");
                    return
                }
            }
            var b = questionMap[a.id];
            var e = new Array();
            $("[id^=qleftanswer_" + a.id + "_]").each(function(h) {
                if ($(this).hasClass("dropReq-gray")) {} else {
                    var i = getAnswerFromQuestion(b, $(this).attr("id").split("_")[2]);
                    e.push(i)
                }
            });
            editFlag = true;
            b.answers = e;
            questionMap[a.id] = b;
            if (a.type == 6 && a.typeid == 5) {
                $("#q_answers_" + b.id).html(getSingleOpenAnswerHtml(b))
            } else {
                $("#q_answers_" + b.id).html(getQuestionAnswerHtml(b))
            }
        }
    })
}
function showQuestionSort(a) {
    $("#qleftquestions").sortable({
        cancel: ".nodrag,:input",
        handle: ".faDel",
        revert: true,
        update: function(c, e) {
            var b = questionMap[a.id];
            var d = new Array();
            $("[id^=qleftquestion_" + a.id + "_]").each(function(f) {
                var g = getQuestionFromQuestion(b, $(this).attr("id").split("_")[2]);
                d.push(g)
            });
            editFlag = true;
            b.questions = d;
            questionMap[a.id] = b;
            $("#q_answers_" + b.id).html(getQuestionAnswerHtml(b))
        }
    })
}
function scrollQuestion(d) {
    if ($(".formButtons-txt").offset().top < $(window).height()) {
        var a = $(".formButtons-txt").offset().top;
        var c = $("#question_" + d).offset().top + $("#question_" + d).height();
        if (a < c) {
            var b = $("#question_" + d).offset().top;
            b = $(document).height() - $(window).height() - $(".footer").height();
            $(document).scrollTop(b)
        }
    } else {
        var b = $(document).height() - $(window).height() - $(".footer").height();
        $(document).scrollTop(b)
    }
}

// 复制问题
function copyQuestion(d) {
    editFlag = true; // 进入编辑状态
    var b = questionMap[d]; // 问题所有属性获取
    var c = cloneQuestion(b); // 复制问题
    var a = "";
    a += getRightShowHtml(c);
    questionMap[c.id] = c;
    $("#question_" + d).after(a);
    refreshNO();
    refreshQuestionHeight(c.id);
    scrollQuestion(c.id);
    showLeftQuestion(c.id);
    chkQestionCnt15()
}

// 对原始问题进行复制
function cloneQuestion(b) {
    var k = (new Date()).getTime();
    var j = {};
    for (var d in b) { // 复制属性
        j[d] = b[d]
    }
    j.id = k; // 更改ID
    if (j.showtype == "question") { // 展示的是问题
        var e = 0;
        if (j.answers != null) { // 复制答案
            var f = new Array();
            for (var d = 0; d < j.answers.length; d++) {
                var h = new Answer(j.answers[d]);
                h.id = k + e;
                f.push(h);
                e++
            }
            j.answers = f
        } else {
            j.answers = null
        }
        if (j.questions != null) { // 复制子问题
            var c = new Array();
            for (var d = 0; d < j.questions.length; d++) {
                var g = new Question(j.questions[d]);
                g.id = k + e;
                c.push(g);
                e++
            }
            j.questions = c
        } else {
            j.questions = null
        }
    } else {
        j.type = 0
    }
    return j
}

function checkDeleteQuestion(a) {
    if (oldQuestionMap[a] == 1 || oldQuestionMap[a] != 1 && editQuestionMap[a] == 1) {
        return true
    } else {
        return false
    }
}

//删除问题
function deleteQuestion(b) {
    var a = questionMap[b]; // 获取问题
    if (a.showtype == "page") { // 删除的是分页
        checkBreakLogic(b,  // 进行逻辑判断操作的影响
        function() {},
        function() {
            deleteQuestionAction(b)
        })
    } else {
        deleteQuestionAction(b)
    }
}
function deleteQuestionAction(a) {
    if (!checkDeleteQuestion(a)) {
        deleteQuestionActionHtml(a)
    } else {
        artconfirm("确定删除？", 
        function() {
            deleteQuestionActionHtml(a)
        },
        function() {},
        "确定", "取消")
    }
}
function deleteQuestionActionHtml(a) {
    editFlag = true;
    targetid = "";
    $("#question_" + a).remove();
    $("#qeditshow").html("");
    questionMap[a] = {};
    refreshNO();
    $("#qeditshow").hide();
    $("#listButtons").hide();
    showNoFieldPosition();
    if ($("[id^=question_]").length == 0) {
        $("#sortable").html(getDefaultHtml())
    }
};